package com.cjz.rxjavalearn;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.cjz.rxjavalearn.api.API;
import com.cjz.rxjavalearn.pojo.Top250MovieData;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends Activity implements MovieListFragment.OnListFragmentInteractionListener {

    private TextView tvDailyData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tvDailyData = (TextView) findViewById(R.id.tvDailyData);
        tvDailyData.setMovementMethod(new ScrollingMovementMethod());


        MovieListFragment movieListFragment = MovieListFragment.newInstance();
        tvDailyData.setOnClickListener(view -> getMovieData(movieListFragment));

        getFragmentManager()
                .beginTransaction()
                .add(R.id.fl_main, movieListFragment)
                .commit();
    }

    private void getMovieData(MovieListFragment movieListFragment) {

//        RetrofitDemoApp.getApp()
//                .getRetrofit()
//                .create(API.GetMoviesAPI.class)
//                .getMovies("v2", 0, 20)
//                .enqueue(new Callback<Top250MovieData>() {
//                    @Override
//                    public void onResponse(Call<Top250MovieData> call, Response<Top250MovieData> response) {
//                        if (response.isSuccessful()) {
//                            Top250MovieData data = response.body();
//
//                            //T0D0 ...bulabula...
//
//                            tvDailyData.setText(data.toString());
//                        }
//                    }
//
//                    @Override
//                    public void onFailure(Call<Top250MovieData> call, Throwable t) {
//
//                    }
//                })
//        ;


        RetrofitDemoApp.getApp().getRetrofit()
                .create(API.GetMoviesAPI.class)
                .getMovies("v2", 0, 20)
                //.filter(top250MovieData -> top250MovieData.getCount() <= 20)
                //.switchIfEmpty(Observable.error(new RuntimeException("this page movie count > page count 20")))
                .debounce(3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(

                        //进行局部刷新的再改造--带延时的列表刷新示例
                        top250MovieData ->
                                Observable.interval(1, TimeUnit.SECONDS)
                                        .take(top250MovieData.getSubjects().size())
                                        .subscribeOn(Schedulers.computation())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(
                                                (index) -> movieListFragment.partialRefresh(top250MovieData.getSubjects().get(index.intValue())),
                                                (error) -> tvDailyData.setText(error.getMessage()),
                                                () -> Log.e("cjz", "locally refresh complete!")
                                        ),

                        (error) -> tvDailyData.setText(error.getMessage()),

                        () -> Log.e("cjz", "net work complete!")
                );
    }

    @Override
    public void onListFragmentInteraction(Top250MovieData.SubjectsBean item) {
        Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
    }
}
